package cn.hnucm.orderservice.controller;


import cn.hnucm.orderservice.entity.TbGoodsOrder;
import cn.hnucm.orderservice.service.TbGoodsOrderService;
import cn.hnucm.orderservice.utils.MyLog;
import cn.hnucm.orderservice.utils.Result;
import cn.hnucm.orderservice.utils.ResultCode;
import cn.hnucm.vo.AddOrder;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-06
 */
@Api(tags = "订单表")
@RestController
@RequestMapping("/orderservice/tbGoodsOrder")
public class TbGoodsOrderController {

    @Autowired
    private TbGoodsOrderService tbGoodsOrderService;

    /**
     * 根据订单ID获取订单
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单ID获取订单")
    @GetMapping("/getOrderById/{orderId}")
    @ResponseBody
    @MyLog("根据订单ID获取订单")
    public Result getOrderById(@PathVariable("orderId") Integer orderId){
        if (orderId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        TbGoodsOrder goodsOrder = tbGoodsOrderService.getOrderById(orderId);
        if (goodsOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok("goodsOrder",goodsOrder);
    }


    /**
     * 根据订单ID获取订单Vo
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单ID获取订单Vo")
    @GetMapping("/getOrderByIdVo/{orderId}")
    @ResponseBody
    @MyLog("根据订单ID获取订单Vo")
    public TbGoodsOrder getOrderByIdVo(@PathVariable("orderId") Integer orderId){
        if (orderId == null) {
            return null;
        }
        try {
            return tbGoodsOrderService.getOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 获取有关于某商品的所有订单
     * @param goodsId
     * @return
     */
    @ApiOperation("获取有关于某商品的所有订单")
    @GetMapping("/getAboutGoodAllOrder/{goodsId}")
    @ResponseBody
    @MyLog("获取有关于某商品的所有订单")
    public Result getAboutGoodAllOrder(@PathVariable("goodsId") Integer goodsId){
        if (goodsId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> goodsOrders = tbGoodsOrderService.getAboutGoodAllOrder(goodsId);
        if (goodsOrders == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goodsOrders.isEmpty()){
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goodsOrders",goodsOrders);
    }

    /**
     * 获取某人的所有订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人的所有订单")
    @GetMapping("/getOneUserAllOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人的所有订单")
    public Result getOneUserAllOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> userAllOrder = tbGoodsOrderService.getOneUserAllOrder(userId);
        if (userAllOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (userAllOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userAllOrder",userAllOrder);
    }


    /**
     * /获取某人卖出的商品的所有订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人卖出的商品的所有订单")
    @GetMapping("/getOneUserSellerOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人卖出的商品的所有订单")
    public Result getOneUserSellerOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> userSellerOrder = tbGoodsOrderService.getOneUserSellerOrder(userId);
        if (userSellerOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (userSellerOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userSellerOrder",userSellerOrder);
    }

    /**
     * 获取某人买来的商品的所有订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人买来的商品的所有订单")
    @GetMapping("/getOneUserbuyOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人买来的商品的所有订单")
    public Result getOneUserbuyOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> userBuyOrder = tbGoodsOrderService.getOneUserbuyOrder(userId);
        if (userBuyOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (userBuyOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userBuyOrder",userBuyOrder);
    }


    /**
     * 获取某人未付款的订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人未付款的订单")
    @GetMapping("/getNoPayOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人未付款的订单")
    public Result getNoPayOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> noPayOrder = tbGoodsOrderService.getNoPayOrder(userId);
        if (noPayOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (noPayOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("noPayOrder",noPayOrder);
    }

    /**
     * 获取某人已付款的订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人已付款的订单")
    @GetMapping("/getPayOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人已付款的订单")
    public Result getPayOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> payOrder = tbGoodsOrderService.getPayOrder(userId);
        if (payOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (payOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("payOrder",payOrder);
    }

    /**
     * 获取某人已送达的订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人已送达的订单")
    @GetMapping("/getSendOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人已送达的订单")
    public Result getSendOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> sendOrder = tbGoodsOrderService.getSendOrder(userId);
        if (sendOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (sendOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("sendOrder",sendOrder);
    }

    /**
     * 获取某人已经结束的订单
     * @param userId
     * @return
     */
    @ApiOperation("获取某人已经结束的订单")
    @GetMapping("/getOverOrder/{userId}")
    @ResponseBody
    @MyLog("获取某人已经结束的订单")
    public Result getOverOrder(@PathVariable("userId") Integer userId){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsOrder> overOrder = tbGoodsOrderService.getOverOrder(userId);
        if (overOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (overOrder.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("overOrder",overOrder);
    }

    /**
     * 获取某人某段时间的所有订单
     * @param userId
     * @param beginTime
     * @param endTime
     * @return
     */
    @ApiOperation("获取某人某段时间的所有订单")
    @ResponseBody
    @GetMapping("/getOneTimeOrder/{userId}/{beginTime}/{endTime}")
    @MyLog("获取某人某段时间的所有订单")
    public Result getOneTimeOrder(@PathVariable("userId") Integer userId,
                                  @PathVariable("beginTime") String beginTime,
                                  @PathVariable("endTime") String endTime){
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        if (beginTime == null) {
            beginTime ="1900-01-01";
        }
        if (endTime == null) {
            endTime = "9999-12-31";
        }
        List<TbGoodsOrder> oneTimeOrder = tbGoodsOrderService.getOneTimeOrder(userId, beginTime, endTime);
        if (oneTimeOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (oneTimeOrder.isEmpty()){
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("oneTimeOrder", oneTimeOrder);
    }

    /**
     * 增加一条订单
     * @param addOrder
     * @return
     */
    @ApiOperation("增加一条订单")
    @ResponseBody
    @PostMapping("/addOrder")
    @MyLog("增加一条订单")
    public Result addOrder(@RequestBody AddOrder addOrder){
        if (addOrder == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        TbGoodsOrder goodsOrder = tbGoodsOrderService.addOrder(addOrder);
        if (goodsOrder == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok("goodsOrder",goodsOrder);
    }



    /**
     * 增加一条订单vo
     * @param addOrder
     * @return
     */
    @ApiOperation("增加一条订单vo")
    @ResponseBody
    @PostMapping("/addOrderVo")
    @MyLog("增加一条订单vo")
    public TbGoodsOrder addOrderVo(@RequestBody AddOrder addOrder){
        if (addOrder == null) {
            return null;
        }
        try {
            return tbGoodsOrderService.addOrder(addOrder);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }




    /**
     * 订单已完成
     * @param orderId
     * @return
     */
    @ApiOperation("订单已完成")
    @ResponseBody
    @GetMapping("/overOrder/{orderId}")
    @MyLog("订单已完成")
    public Result overOrder(@PathVariable("orderId") Integer orderId){
        if (orderId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = tbGoodsOrderService.overOrder(orderId);
        if (aBoolean == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (!aBoolean) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok();
    }

    /**
     * 订单已完成Vo
     * @param orderId
     * @return
     */
    @ApiOperation("订单已完成Vo")
    @ResponseBody
    @GetMapping("/overOrderVo/{orderId}")
    @MyLog("订单已完成Vo")
    public TbGoodsOrder overOrderVo(@PathVariable("orderId") Integer orderId){
        if (orderId == null) {
            return null;
        }
        try {
            Boolean aBoolean = tbGoodsOrderService.overOrder(orderId);
            return aBoolean==null||!aBoolean?null:tbGoodsOrderService.getOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }



    /**
     * 通过id删除一条订单
     * @param orderId
     * @return
     */
    @ApiOperation("通过id删除一条订单")
    @ResponseBody
    @GetMapping("/deleteOrderById/{orderId}")
    @MyLog("通过id删除一条订单")
    public Result deleteOrderById(@PathVariable("orderId") Integer orderId){
        if (orderId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = tbGoodsOrderService.deleteOrderById(orderId);
        if (aBoolean == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (!aBoolean) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok();
    }

    /**
     * 通过id删除一条订单Vo
     * @param orderId
     * @return
     */
    @ApiOperation("通过id删除一条订单Vo")
    @ResponseBody
    @GetMapping("/deleteOrderByIdVo/{orderId}")
    @MyLog("通过id删除一条订单")
    public Boolean deleteOrderByIdVo(@PathVariable("orderId") Integer orderId){
        if (orderId == null) {
            return null;
        }
        try {
            return tbGoodsOrderService.deleteOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过id取消订单Vo
     * @param orderId
     * @return
     */
    @ApiOperation("通过id取消订单Vo")
    @ResponseBody
    @GetMapping("/cancelOrderVo/{orderId}")
    @MyLog("通过id取消订单Vo")
    public TbGoodsOrder cancelOrderVo(@PathVariable("orderId") Integer orderId){
        if (orderId == null){
            return null;
        }
        try {
            TbGoodsOrder goodsOrder = new TbGoodsOrder();
            goodsOrder.setOrderId(orderId);
            //5 为订单取消状态
            goodsOrder.setOrderState(5);
            boolean b = tbGoodsOrderService.updateById(goodsOrder);
            return !b?null:tbGoodsOrderService.getOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}

